package cn.iocoder.yudao.module.system.convert.logger;

import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.logger.vo.operatelog.OperateLogRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.SUCCESS;

/**
 * 操作日志转换
 *
 * @author huangweidong
 * @date 2023/05/09
 */
@Mapper
public interface OperateLogConvert {

    OperateLogConvert INSTANCE = Mappers.getMapper(OperateLogConvert.class);

    /**
     * 转换
     *
     * @param bean bean
     * @return {@code OperateLogDO}
     */
    OperateLogDO convert(OperateLogCreateReqDTO bean);

    /**
     * 转换分页
     *
     * @param page 分页
     * @return {@code PageResult<OperateLogRespVO>}
     */
    PageResult<OperateLogRespVO> convertPage(PageResult<OperateLogDO> page);

    /**
     * 转换
     *
     * @param bean bean
     * @return {@code OperateLogRespVO}
     */
    OperateLogRespVO convert(OperateLogDO bean);

    /**
     * 转换列表
     *
     * @param list    列表
     * @param userMap 用户映射
     * @return {@code List<OperateLogExcelVO>}
     */
    default List<OperateLogExcelVO> convertList(List<OperateLogDO> list, Map<Long, AdminUserDO> userMap) {
        return list.stream().map(operateLog -> {
            OperateLogExcelVO excelVO = convert02(operateLog);
            MapUtils.findAndThen(userMap, operateLog.getUserId(), user -> excelVO.setUserNickname(user.getNickname()));
            excelVO.setSuccessStr(SUCCESS.getCode().equals(operateLog.getResultCode()) ? "成功" : "失败");
            return excelVO;
        }).collect(Collectors.toList());
    }

    /**
     * convert02
     *
     * @param bean bean
     * @return {@code OperateLogExcelVO}
     */
    OperateLogExcelVO convert02(OperateLogDO bean);

}
